const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : `0${n}`
}

/**
 * 计算两个字符串时间的相差的毫秒数
 * @param {STRING} start 开始时间 2022-01-02 09:00
 * @param {STRING} end 结束的时间 2022-01-02 10:00
 */
function getTimeDifference(start, end) {
	const s1 = (new Date(start)).valueOf()
	const s2 = (new Date(end)).valueOf()
	const sT = s2 - s1
	const H = Math.floor(sT / 3600000)
	const M = Math.floor(sT % 3600000 / 60000)
	const S = Math.floor(sT % 3600000 % 60000 / 1000)
	return {
		secends: (s2 - s1) / 1000,
		T: `${H < 10 ? '0' + H : H}:${M < 10 ? '0' + M : M}:${S < 10 ? '0' + S : S}`
	}
}


module.exports = {
	formatTime,
	getTimeDifference
}
